using System;
using System.Collections.Generic;
using SKBKontur.Logging.Model;

namespace SKBKontur.Logging
{
    public class LevelDispatchingLogEventWriter : ILogEventWriter
    {
        private readonly Dictionary<LogEventLevel, ILogEventWriter> configuration = new Dictionary<LogEventLevel, ILogEventWriter>();

        public LevelDispatchingLogEventWriter(ILevelDispatchingLogEventWriterConfigurator configurator)
        {
            foreach (var level in Enum.GetValues(typeof(LogEventLevel)))
            {
                var l = (LogEventLevel) level;
                var writer = configurator[l];
                if (writer == null)
                    continue;
                configuration[l] = writer;
            }
        }

        public void Create<T>(T logEvent) where T : LogEvent
        {
            if (!configuration.ContainsKey(logEvent.EventLevel))
                return;
            configuration[logEvent.EventLevel].Create(logEvent);
        }
    }
}